using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;


namespace KFGame
{
    /// <summary>
    /// 数据管理系统，用于带统计的数据类型， 积分，投币等数据
    /// </summary>
    public class KFDatasManager
    {
        //数据结构
        public struct KFData
        {
            public DataType type;
            public string name;
            public long value;
        }

        //计算单位
        public enum DataType
        {
            poin = 0,//实时点为计数点
            day, //天计数点
            week, //周为计数点
            month, //月为计数点
            year, //年为计数点
            total,//总数为计数点

        }

        /// <summary>
        /// 转换数据值列表
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public List<long> datasValue(List<KFData> ds)
        {
            List<long> ls = new List<long>();
            foreach (var i in ds)
                ls.Add(i.value);

            return ls;
        }

        /// <summary>
        ///  读取一个数据
        /// </summary>
        /// <param name="name">数据的名字</param>
        /// <param name="type">计算单位</param>
        /// <param name="Offset">偏移，负数前移，正数后移</param>
        public KFData getData(string name, DataType type, double Offset = 0)
        {
            KFData data = new KFData();
            switch (type)
            {
                case DataType.poin:
                    break;
                case DataType.day:
                    DaysLog days = new DaysLog(name);
                    days.load(Offset);

                    break;
                case DataType.week:
                    break;
                case DataType.month:
                    MonthLog month = new MonthLog(name);
                    //  month.load();
                    month.setMonth((int)Offset);
                    month.load();

                    break;
                case DataType.year:
                    YearLog year = new YearLog(name);
                    year.load((int)Offset);

                    break;
                case DataType.total:
                    TotalLog total = new TotalLog(name);
                    total.load();

                    break;
            }

            return data;
        }

        /// <summary>
        /// 读取一组数据 
        /// </summary>
        /// <param name="name">数据的名字</param>
        /// <param name="type">计算单位</param>
        /// <param name="langth">offset 偏移向前多少个单位</param>
        /// <param name="Offset">偏移，负数前移，正数后移</param>
        /// <returns></returns>
        public List<KFData> getDatas(string name, DataType type, int langth = 1, double Offset = 0)
        {


            return new List<KFData>();
        }

        /// <summary>
        /// 设置一个指定的数值 
        /// </summary>
        /// <param name="name">数据的名字</param>
        /// <param name="type">计算单位</param>
        /// <param name="value">设定的值</param>
        public void setData(string name, DataType type, long value)
        {
            switch (type)
            {
                case DataType.poin:
                    break;
                case DataType.day:
                    DaysLog days = new DaysLog(name);
                    // days.load();
                    days.addDatas(value);
                    days.save();
                    break;
                case DataType.week:
                    break;
                case DataType.month:
                    MonthLog month = new MonthLog(name);
                    //  month.load();
                    month.addDatas(value);
                    month.save();
                    break;
                case DataType.year:
                    YearLog year = new YearLog(name);
                    // year.load();
                    year.addDatas(value);
                    year.save();
                    break;
                case DataType.total:
                    TotalLog total = new TotalLog(name);
                    // total.load();
                    total.addDatas(value);
                    total.save();
                    break;
            }
        }

        /// <summary>
        /// 添加一个数值 ，累加计算
        /// </summary>
        /// <param name="name">数据的名字</param>
        /// <param name="type">计算单位</param>
        /// <param name="value">加的值 </param>
        public void addData(string name, DataType type, long value)
        {
            switch (type)
            {
                case DataType.poin:
                    break;
                case DataType.day:
                    DaysLog days = new DaysLog(name);
                    days.load();
                    days.addDatas(value);
                    days.save();
                    break;
                case DataType.week:
                    break;
                case DataType.month:
                    MonthLog month = new MonthLog(name);
                    month.load();
                    month.addDatas(value);
                    month.save();
                    break;
                case DataType.year:
                    YearLog year = new YearLog(name);
                    year.load();
                    year.addDatas(value);
                    year.save();
                    break;
                case DataType.total:
                    TotalLog total = new TotalLog(name);
                    total.load();
                    total.addDatas(value);
                    total.save();
                    break;
            }
        }

        /// <summary>
        /// 添加一个数值 ，累加计算,累加到所有类型
        /// </summary>
        /// <param name="name">数据的名字</param>
        /// <param name="value">加的值</param>
        public void addData(string name, long value)
        {
            addData(name, DataType.poin, value);
            addData(name, DataType.day, value);
            addData(name, DataType.week, value);
            addData(name, DataType.month, value);
            addData(name, DataType.year, value);
            addData(name, DataType.total, value);
        }


        /// <summary>
        ///  清空数据
        /// </summary>
        /// <param name="name">数据的名字</param>
        /// <param name="type">计算单位</param>
        public void clean(string name, DataType type)
        {

        }

        /// <summary>
        /// 清空所有数据
        /// </summary>
        /// <param name="name">清空指定名字的数据，如果为空为所有数据</param>
        public void cleanAll(string name = null)
        {
            if (name == null)
            {
                KFDataFile df = new KFDataFile();
                DeleteFiles(df.dir);
                return;
            }

        }
        public bool DeleteFiles(string path)
        {
            if (Directory.Exists(path) == false)
                return false;

            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] files = dir.GetFiles();
            try
            {
                foreach (var item in files)
                {
                    File.Delete(item.FullName);
                }
                // if (dir.GetDirectories().Length != 0)
                // {
                //     foreach (var item in dir.GetDirectories())
                //     {
                //         if (!item.ToString().Contains("$") && (!item.ToString().Contains("Boot")))
                //         {
                //             // Console.WriteLine(item);

                //             DeleteFiles(dir.ToString() + "\\" + item.ToString());
                //         }
                //     }
                // }
                //Directory.Delete(path);

                return true;
            }
            catch (Exception)
            {
                return false;

            }




        }
    }
}